<?php
declare (strict_types = 1);

namespace app\index\model\charge;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class ChargingStation extends Model
{
    protected $pk = "id";

    /**
     * 获取站点信息信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id,$field="*"){
        return $this->where('id',$id)->field($field)->find();
    }

    /**
     * 推荐站点列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page=1,$limit=15,$field="*"){
        $where = ['is_delete'=>1,'is_status'=>1,];
        $sql = Db::table('jy_charging_station')->where($where);
        $countSql = Db::table('jy_charging_station')->where($where);
        //街镇
        if(isset($getData['street_id']) && $getData['street_id']){
            $sql = $sql->where('street_id',$getData['street_id']);
            $countSql = $countSql->where('street_id',$getData['street_id']);
        //区域
        }else if(isset($getData['district_id']) && $getData['district_id']){
            $sql = $sql->where('district_id',$getData['district_id']);
            $countSql = $countSql->where('district_id',$getData['district_id']);
        //城市
        }else if(isset($getData['city_id']) && $getData['city_id']){
            $sql = $sql->where('city_id',$getData['city_id']);
            $countSql = $countSql->where('city_id',$getData['city_id']);
        //省份
        }else if(isset($getData['province_id']) && $getData['province_id']){
            $sql = $sql->where('province_id',$getData['province_id']);
            $countSql = $countSql->where('province_id',$getData['province_id']);
        }
        //地址
        if(isset($getData['address']) && $getData['address'] && !isset($getData['select_status'])){
            $sql = $sql->whereOr('address','like','%'.$getData['address'].'%');
            $countSql = $countSql->whereLike('address','%'.$getData['address'].'%');
        }
        //站点名称
        if(isset($getData['title']) && $getData['title']  && !isset($getData['select_status'])){
            $sql = $sql->whereLike('title','%'.$getData['title'].'%');
            $countSql = $countSql->whereLike('title','%'.$getData['title'].'%');
        }
        //站点和地址同时查询
        if(isset($getData['address']) && $getData['address'] && isset($getData['select_status']) && isset($getData['title'])){
            $sql = $sql->where(function ($query)use($getData){
                $query->where('address','like','%'.$getData['address'].'%')->whereOr('title','like','%'.$getData['title'].'%');
            });
            $countSql = $countSql->where(function ($query)use($getData){
                $query->where('address','like','%'.$getData['address'].'%')->whereOr('title','like','%'.$getData['title'].'%');
            });
        }
        if(isset($getData['lat']) && isset($getData['lng']) && $getData['lat'] && $getData['lng'] ){
            // 数据库字段名 - 纬度
            $field_lat = 'latitude';
            // 数据库字段名 - 经度
            $field_lng = 'longitude';
            $sql = $sql->field($field.", (6378.138 * 2 * asin(sqrt(pow(sin(({$field_lat} * pi() / 180 - {$getData['lat']} * pi() / 180) / 2),2) + cos({$field_lat} * pi() / 180) * cos({$getData['lat']} * pi() / 180) * pow(sin(({$field_lng} * pi() / 180 - {$getData['lng']} * pi() / 180) / 2),2))) * 1000) as distance")->order('distance','asc');
        }else{
            $sql = $sql->field($field)->order('system_recommend','desc');
        }
        $count = $countSql->count();
        $data = $sql->page($page,$limit)->select()->toArray();
        $list = $this->setStationData($data);
        return compact('count','list');
    }

    /**
     * 我收藏的站点
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function myCollectionStation($getData,$page,$limit,$field="*"){
        $where = ['s.is_delete'=>1,'s.is_status'=>1,'u.user_id'=>$getData['user_id']];
        $sql = Db::table('jy_user_station_collection')->alias('u')->leftJoin('jy_charging_station s','u.station_id = s.id')->where($where);
        $countSql = Db::table('jy_user_station_collection')->alias('u')->leftJoin('jy_charging_station s','u.station_id = s.id')->where($where);
        //街镇
        if(isset($getData['street_id']) && $getData['street_id']){
            $sql = $sql->where('s.street_id',$getData['street_id']);
            $countSql = $countSql->where('s.street_id',$getData['street_id']);
            //区域
        }else if(isset($getData['district_id']) && $getData['district_id']){
            $sql = $sql->where('s.district_id',$getData['district_id']);
            $countSql = $countSql->where('s.district_id',$getData['district_id']);
            //城市
        }else if(isset($getData['city_id']) && $getData['city_id']){
            $sql = $sql->where('s.city_id',$getData['city_id']);
            $countSql = $countSql->where('s.city_id',$getData['city_id']);
            //省份
        }else if(isset($getData['province_id']) && $getData['province_id']){
            $sql = $sql->where('s.province_id',$getData['province_id']);
            $countSql = $countSql->where('s.province_id',$getData['province_id']);
        }
        //地址
        if(isset($getData['address']) && $getData['address']){
            $sql = $sql->whereLike('address','%'.$getData['address'].'%');
            $countSql = $countSql->whereLike('s.address','%'.$getData['address'].'%');
        }
        //充电站名称
        if(isset($getData['title']) && $getData['title']){
            $sql = $sql->whereLike('title','%'.$getData['title'].'%');
            $countSql = $countSql->whereLike('s.title','%'.$getData['title'].'%');
        }
        if(isset($getData['lat']) && isset($getData['lng']) && $getData['lat'] && $getData['lng'] ){
            // 数据库字段名 - 纬度
            $field_lat = 'latitude';
            // 数据库字段名 - 经度
            $field_lng = 'longitude';
            $sql = $sql->field($field.",u.station_id,u.user_id, (6378.138 * 2 * asin(sqrt(pow(sin(({$field_lat} * pi() / 180 - {$getData['lat']} * pi() / 180) / 2),2) + cos({$field_lat} * pi() / 180) * cos({$getData['lat']} * pi() / 180) * pow(sin(({$field_lng} * pi() / 180 - {$getData['lng']} * pi() / 180) / 2),2))) * 1000) as distance")->order(['distance'=>'sac','system_recommend'=>'desc']);
        }else if(isset($getData['set_order']) && $getData['set_order']){
            $sql = $sql->field($field)->order($getData['set_order']);
        }else{
            $sql = $sql->field($field)->order(['system_recommend'=>'desc']);
        }
        $count = $countSql->count();
        $data = $sql->page($page,$limit)->select()->toArray();
        $list = $this->setStationData($data);
        return compact('count','list');
    }

    /**
     * 设置返回数据
     * @param $data
     * @return mixed
     */
    function setStationData($data){
        $openStatus = [0=>'不对外开发',1=>'对外开放'];
        $chargePileGunModel = new ChargePileGun();
        $chargePileModel = new ChargePile();
        foreach ($data as $key=>$item){
            //设置返回的距离
            if(isset($item['distance'])){
                if($item['distance'] > 1000){
                    $item['distance'] = number_format($item['distance']/1000,2)."km";
                }else{
                    $item['distance'] = number_format($item['distance'],1)."m";
                }
            }else{
                $item['distance'] = 0;
            }
            //站点图标
            $item['icon_img'] = config("index.web_url").($item['icon_img'] ? $item['icon_img'] : config('index.icon_img'));
            $item['remark_info']= $openStatus[$item['is_open']];
            if($item['max_power']) $item['remark_info']= $item['remark_info']." | ".$item['max_power'];
            if($item['station_remark']) $item['remark_info']= $item['remark_info']." | ".$item['station_remark'];
            //获取站点枪的信息
            $item['gun_count'] = $chargePileGunModel->getStationGunCount($item['id']);
            //获取当前的价格
            $item['gun_cost'] = $chargePileModel->getPileChargeCost($item['id']);
            $data[$key] = $item;
        }
        return $data;
    }

    /**
     * 获取地区列表
     * @param $where
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getAllList($where,$field='*'){
        return $this->where($where)->field($field)->select()->toArray();
    }

}
